package job.sqb;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Author: Zhang Dongwei
 * Date: 2023/9/9 13:48
 */
public class sqb2 {
    public static String[] transformAndSort(String s, String[] strs) {
        Map<Character, Integer> charCountMap = new HashMap<>();
        for (char c : s.toCharArray()) {
            charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
        }

        Arrays.sort(strs, (a, b) -> {
            String transformedA = transformString(a, charCountMap);
            String transformedB = transformString(b, charCountMap);
            return transformedA.compareTo(transformedB);
        });

        return strs;
    }

    private static String transformString(String str, Map<Character, Integer> charCountMap) {
        StringBuilder transformed = new StringBuilder();
        for (char c : str.toCharArray()) {
            transformed.append(charCountMap.getOrDefault(c, 0));
        }
        return transformed.toString();
    }

    public static void main(String[] args) {
        String s = "apple";
        String[] strs = {"plap", "aple", "elpa", "pale", "apple"};

        String[] sortedStrs = transformAndSort(s, strs);
        System.out.println(Arrays.toString(sortedStrs));
    }
}
